package com.xsd.awen.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xsd.awen.config.WebResult;
import com.xsd.awen.domain.entity.Task;
import com.xsd.awen.domain.entity.UserTask;
import com.xsd.awen.domain.po.UserTaskPagePo;
import com.xsd.awen.domain.vo.UserTaskUpdateVo;
import com.xsd.awen.domain.vo.UserTaskVo;
import com.xsd.awen.mapper.UserTaskMapper;
import com.xsd.awen.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Scope(proxyMode= ScopedProxyMode.TARGET_CLASS)
public class UserTaskService {

    @Autowired
    UserTaskMapper userTaskMapper;

    public PageInfo<UserTaskPagePo> page(UserTaskVo userTaskVo){
        PageHelper.startPage(userTaskVo.getPage(),userTaskVo.getLimit());
        return new PageInfo<>(userTaskMapper.page(userTaskVo));
    }

    @Transactional
    public String save(UserTaskUpdateVo vo){
        UserTask ut = new UserTask();
        ut.setTaskId(vo.getTaskId());
        userTaskMapper.deleteByTaskId(ut);

        for( UserTask userTask : vo.getUserTasks() ){
            userTask.setId(UUIDUtils.generate());
            userTask.setCreateUserId(vo.getCreateUserId());
            userTask.setTaskId(vo.getTaskId());
            userTaskMapper.insert(userTask);
        }
        return WebResult.OK();
    }

    public String update(UserTaskUpdateVo vo){
        return WebResult.OK();
    }

    public String delete(UserTask userTask){
        userTaskMapper.deleteById(userTask);
        return WebResult.OK();
    }

    public List<UserTask> get(Task task){
        return userTaskMapper.get(task);
    }

}
